package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.activity.ghost.GhostBossData;
import com.yanqu.road.entity.activity.ghost.GhostData;
import com.yanqu.road.entity.activity.ghost.GhostDispersePlayer;
import com.yanqu.road.entity.activity.ghost.GhostSimplePlayer;
import com.yanqu.road.entity.activity.ghost.GhostUnionNews;
import com.yanqu.road.entity.activity.ghost.config.GhostBossConfig;
import com.yanqu.road.entity.activity.ghost.config.GhostConfig;
import com.yanqu.road.pb.activity.GhostActivityProto;

import java.util.Map;


public class GhostPb {

    public static GhostActivityProto.TodayGhostDataMsg.Builder parseTodayGhostDataMsg(GhostData data, GhostConfig config) {
        GhostActivityProto.TodayGhostDataMsg.Builder builder = GhostActivityProto.TodayGhostDataMsg.newBuilder();
        builder.setBossCount(data.getBossCount().intValue());
        builder.setBoxCount(0); // 取消数量限制

        if (data.getDispersePlayerMap().containsKey(data.getMaxBatchNo())) {
            // 最终boss被发现才设置值
            GhostDispersePlayer findPlayer = data.getDispersePlayerMap().get(data.getMaxBatchNo()).get(1);
            if (findPlayer != null) {
                for (GhostBossConfig bossConfig : config.getBossConfigList()) {
                    if (bossConfig.isBigBoss()) {
                        Map<Integer, GhostBossData> dataMap = data.getBossMap().get(data.getMaxBatchNo()).get(bossConfig.getBossType());
                        for (GhostBossData bossData : dataMap.values()) {
                            // 设置boss数据
                            builder.setBigBossData(parseGhostBossDataMsg(bossData));
                            // 顺便设置发现者信息
                            builder.setFindPlayer(PlayerBasePb.parsePlayerBaseTempMsg(findPlayer.getUserBaseInfo()));
                            break;
                        }
                    }
                }
            }
            GhostDispersePlayer killPlayer = data.getDispersePlayerMap().get(data.getMaxBatchNo()).get(2);
            if (killPlayer != null) {
                builder.setKillPlayer(PlayerBasePb.parsePlayerBaseTempMsg(killPlayer.getUserBaseInfo()));
            }
        }
        return builder;
    }

    public static GhostActivityProto.GhostBossDataMsg.Builder parseGhostBossDataMsg(GhostBossData data) {
        GhostActivityProto.GhostBossDataMsg.Builder builder = GhostActivityProto.GhostBossDataMsg.newBuilder();
        builder.setBossId(data.getBossId());
        builder.setBossType(data.getBossType());
        builder.setTotalHp(data.getTotalHp());
        builder.setCurHp(data.getCurHp());
        if (data.getCurHp() <= 0 && data.getKiller().getUserAliasId() > 0) {
            builder.setKiller(PlayerBasePb.parsePlayerBaseTempMsg(data.getKiller()));
        }
        return builder;
    }

    public static GhostActivityProto.GhostUnionNewsMsg.Builder parseGhostUnionNewsMsg(GhostUnionNews data, GhostBossData bossData) {
        GhostActivityProto.GhostUnionNewsMsg.Builder builder = GhostActivityProto.GhostUnionNewsMsg.newBuilder();
        builder.setUserId(data.getUserId());
        builder.setBossData(parseGhostBossDataMsg(bossData));
        return builder;
    }

    public static GhostActivityProto.GhostSimplePlayerMsg.Builder parseGhostSimplePlayerMsg(GhostSimplePlayer player) {
        GhostActivityProto.GhostSimplePlayerMsg.Builder builder = GhostActivityProto.GhostSimplePlayerMsg.newBuilder();
        builder.setServerId(player.getServerId());
        builder.setUserId(player.getUserId());
        builder.setNickName(player.getNickName());
        builder.setDecoration(player.getDecoration());
        return builder;
    }

    public static GhostActivityProto.GhostSimplePlayerMsg.Builder parseGhostSimplePlayerMsg(long userId, long serverId, String nickname, String decoration) {
        GhostActivityProto.GhostSimplePlayerMsg.Builder builder = GhostActivityProto.GhostSimplePlayerMsg.newBuilder();
        builder.setUserId(userId);
        builder.setServerId(serverId);
        builder.setNickName(nickname);
        builder.setDecoration(decoration);
        return builder;
    }

    public static GhostSimplePlayer parseGhostSimplePlayer(GhostActivityProto.GhostSimplePlayerMsg msg) {
        GhostSimplePlayer player = new GhostSimplePlayer();
        player.setServerId(msg.getServerId());
        player.setUserId(msg.getUserId());
        player.setNickName(msg.getNickName());
        player.setDecoration(msg.getDecoration());
        return player;
    }

    public static GhostSimplePlayer parseGhostSimplePlayer(long userId, long serverId, String nickname, String decoration) {
        GhostSimplePlayer player = new GhostSimplePlayer();
        player.setServerId(serverId);
        player.setUserId(userId);
        player.setNickName(nickname);
        player.setDecoration(decoration);
        return player;
    }


}
